package com.csgc.camera.widget.mask

import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import coil.load

import com.csgc.camera.constants.MaskConfig
import com.csgc.camera.storage.MaskPreference
import com.csgc.camera.util.dp
import com.cysd.spsyxj.R


class MaskCar:MaskBase {

    private var name: TextView?=null
    private var projectName: TextView?=null
    private var peopleName: TextView?=null
    private var cardNum: TextView?=null
    constructor(context: Context?, id:Int) : super(context,null){

        if (id == 0){
            LayoutInflater.from(context).inflate(R.layout.item_mask_car_1, this, true)
        }else if (id == 1){
            LayoutInflater.from(context).inflate(R.layout.item_mask_car_2, this, true)
        }

        dateStyle = "yyyy-MM-dd EEEE"

        if(MaskConfig.maskDateStyle.isNotEmpty()){
            dateStyle = MaskConfig.maskDateStyle
        }

        initView()
    }


    private fun initView(){
        location = findViewById(R.id.tv_car_location)
        remark = findViewById(R.id.tv_car_remark)
        date = findViewById(R.id.tv_car_date)
        customMaskContainer = findViewById(R.id.layout_custom_mask_container)

        name = findViewById(R.id.tv_car_name)
        projectName = findViewById(R.id.tv_car_project)
        peopleName = findViewById(R.id.tv_car_people)
        cardNum = findViewById(R.id.tv_car_card)

        location?.text = MaskConfig.maskLocation
        changeDateTime()


        MaskConfig.customMaskInfo.forEach {
            val text = TextView(context)
            text.setTextColor(Color.WHITE)
            text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14f)
            text.text = "${it.title}:${it.content}"
            text.visibility = if (it.isOpen) View.VISIBLE else View.GONE

            customMaskContainer?.addView(text)
        }

        val maskType = MaskPreference.instance.getMaskType()
        /*MaskConfig.maskMap[maskType]?.forEach {
            if(it.title == "备注"){
                remark?.visibility = if(it.isOpen) View.VISIBLE else View.GONE
            }
        }*/

        MaskConfig.maskMap[maskType]?.forEach {
            if(it.title == "备注"){
                remark?.text = "${it.title}:${it.content}"
                remark?.visibility = if(it.isOpen) View.VISIBLE else View.GONE
            }else if(it.title == "汽车品牌" && it.content.isNotEmpty()){
                name?.text = it.content
            }else if(it.title == "保养项目" && it.content.isNotEmpty()){
                projectName?.text = it.content
            }else if(it.title == "车间技师" && it.content.isNotEmpty()){
                peopleName?.text = it.content
            }else if(it.title == "车牌" && it.content.isNotEmpty()){
                cardNum?.text = it.content
            }
        }


    }

    override fun updateItemContent(title: String, content: String) {
        super.updateItemContent(title, content)
        if(title == "汽车品牌"){
            name?.text = content
        }else if(title == "保养项目"){
            projectName?.text = content
        }else if(title == "车间技师"){
            peopleName?.text = content
        }else if(title == "车牌"){
            cardNum?.text = content
        }
    }
}